<template>
	<view class="address">
		<u-navbar :title="title" @leftClick="rightClick" :autoBack="true" :placeholder="true">
		</u-navbar>

		<view class="dataBox">
			<view class="dataItem">
				<view class="lable">
					收件人
				</view>
				<image src="../../static/image/star.png" style="width: 20rpx;height: 20rpx;" mode=""></image>
				<input class="value" v-model="name" type="text" placeholder="请填写收货人姓名" />

			</view>
			<view class="dataItem">
				<view class="lable">
					手机号
				</view>
				<image src="../../static/image/star.png" style="width: 20rpx;height: 20rpx;" mode=""></image>
				<input class="value" v-model="phone" type="text" placeholder="请填写收货人手机号" />
			</view>
			<view class="dataItem">
				<view class="lable">
					微信号
				</view>
				<input class="value" type="text" style="margin-left: 20rpx;" placeholder="请填写收货人微信号" />
			</view>
			<view class="dataItem">
				<view class="lable">
					其他联系方式
				</view>
				<input class="value" type="text" style="margin-left: 20rpx;" placeholder="请填写其他联系方式" />
			</view>
			<view class="dataItem" @click="show = true">
				<view class="lable">
					所在地区
				</view>
				<image src="../../static/image/star.png" style="width: 20rpx;height: 20rpx;" mode=""></image>
				<!-- <input class="value" v-model="area" type="text" placeholder="省市区县、乡镇等" /> -->
				<view :class="area == ''? 'colornull' : 'value'">
					{{area == "" ?'请选择所在地区' : area}}
				</view>
			</view>
			<view class="dataItem">
				<view class="lable">
					详细地址
				</view>
				<image src="../../static/image/star.png" style="width: 20rpx;height: 20rpx;" mode=""></image>
				<input class="value" v-model="address" type="text" placeholder="街道楼牌号等" />
			</view>

		</view>

		<view class="deflateBox">
			<view class="">
				设为默认地址
			</view>
			<view class="" @click="handleIsdefault">
				<image v-if="isdefault" src="../../static/image/a1.png" style="width: 32rpx;height: 32rpx;" mode="">
				</image>
				<image v-else src="../../static/image/a2.png" style="width: 32rpx;height: 32rpx;" mode=""></image>
			</view>

		</view>

		<view class="btn" @click="handleSave">
			{{handletype}}
		</view>

		<u-picker :show="show" :columns="columns" @confirm="confirmarea" @cancel="show = false"></u-picker>

	</view>
</template>

<script>
	import {
		adduserarea,
		getuserareaone,
		freight
	} from "@/api/api.js"
	export default {
		data() {
			return {
				title: "新增地址",
				isdefault: false,
				name: "", //姓名
				phone: "", //手机号
				area: "", //地址
				address: "", //详细地址
				handletype: "保存新增",
				pageId: "",
				show: false,
				columns: [
					[]
				],
			}
		},
		onLoad(options) {
			console.log("333", options)
			if (options.id) {
				this.title = '编辑地址'
				this.handletype = "保存编辑"
				this.pageId = options.id
				this.handleHxdata()
			}
			this.handlefreight()
		},
		methods: {
			async handlefreight() {
				let res = await freight()
				res.data.forEach((item, index) => {
					this.columns[0].push(item.name)
				})
			},
			confirmarea(e) {
				console.log("44", e)
				this.area = e.value[0]
				this.show = false
			},
			async handleHxdata() {
				let res = await getuserareaone({
					id: this.pageId
				})
				this.name = res.data.name
				this.phone = res.data.phone
				this.area = res.data.area
				this.address = res.data.address
				this.isdefault = res.data.type == '0' ? false : true
			},
			handleIsdefault() {
				this.isdefault = !this.isdefault
			},
			async handleSave() {
				if (this.name == "") {
					uni.showToast({
						title: '请输入收货人姓名！',
						icon: "none",
						duration: 2000
					});
					return
				}
				if (this.phone == "") {
					uni.showToast({
						title: '请输入收货人手机号！',
						icon: "none",
						duration: 2000
					});
					return
				}
				if (this.area == "") {
					uni.showToast({
						title: '请输入收货人所在地址！',
						icon: "none",
						duration: 2000
					});
					return
				}
				if (this.address == "") {
					uni.showToast({
						title: '请输入收货人详细地址！',
						icon: "none",
						duration: 2000
					});
					return
				}
				let data = {
					name: this.name,
					phone: this.phone,
					area: this.area,
					address: this.address,
					type: this.isdefault ? '1' : '0'
				}
				console.log("data", data)
				let res = await adduserarea(data)
				if (res.code == 1000) {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 2000
					});
					uni.navigateBack({
						delta: 1
					})
				} else {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 2000
					});
				}
			},
			rightClick() {},
		}
	}
</script>

<style scoped lang="scss">
	.colornull {
		color: grey;
		font-size: 28rpx;
		margin-left: 10rpx;
	}

	.dataBox {
		margin-top: 20rpx;

		.dataItem {
			width: 100%;
			height: 100rpx;
			background: #FFFFFF;
			margin-bottom: 1rpx;
			padding: 30rpx;
			box-sizing: border-box;
			display: flex;
			align-items: center;

			.lable {
				width: 122rpx;
				font-size: 28rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				color: #333333;
			}

			.value {
				font-size: 28rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				line-height: 33rpx;
				margin-left: 20rpx;
			}
		}

	}

	.deflateBox {
		width: 100%;
		height: 100rpx;
		background: #FFFFFF;
		margin-top: 20rpx;
		padding: 30rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 28rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		color: #333333;
		line-height: 33rpx;
	}

	.btn {
		width: 670rpx;
		height: 80rpx;
		background: #439B05;
		border-radius: 100rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		font-size: 32rpx;
		color: #FFFFFF;
		line-height: 38rpx;
		text-align: center;
		line-height: 80rpx;
		position: absolute;
		bottom: 40rpx;
		left: 50%;
		margin-left: -335rpx;

	}
</style>